﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projectmanagement.Entity;
using Projectmanagement.Utl;
using Telerik.Web.Mvc;

namespace Projectmanagement.Controllers
{
    [RoleRequired]
    public class SimulatorConfigController : Controller
    {
        public ViewResult Index()
        {
            var names = CH.DB.EmployeeRolePMs.OrderBy(c => c.Name).Select(c => c.Name).Distinct();
            ViewBag.names = names;
            return View();
        }

        [GridAction]
        public ActionResult _SelectIndex()
        {
            string loginUserName = Employee.GetLoginUserName();
            SimulatorConfig simulatorConfig =
                CH.DB.SimulatorConfigs.FirstOrDefault(s => s.AdminName == loginUserName);
                    //CH.GetAllData<SimulatorConfig>(s => s.AdminName == loginUserName).SingleOrDefault();
            if (simulatorConfig == null)
            {
                simulatorConfig = new SimulatorConfig();
                simulatorConfig.SimulatorName = loginUserName;
            }

            List<SimulatorConfig> gridList = new List<SimulatorConfig>();
            gridList.Add(simulatorConfig);
            return View(new GridModel(gridList));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ViewResult _SaveAjaxEditing(int id)
        {
            var item = CH.DB.SimulatorConfigs.Find(id);
            //var item = CH.GetDataById<SimulatorConfig>(id);
            if (item == null)
            {
                item = new SimulatorConfig();
                item.AdminName = Employee.GetLoginUserName();
                TryUpdateModel(item);
               
                CH.Create<SimulatorConfig>(item);
            }
            else
            {
                TryUpdateModel(item);
                CH.Edit<SimulatorConfig>(item);
            }
            return View(new GridModel(CH.GetAllDatas<SimulatorConfig>()));
        }

        protected override void Dispose(bool disposing)
        {
            CH.DB.Dispose();
            base.Dispose(disposing);
        }
    }
}
